<template>
  <div class="search-panel">
    <el-row class="m-header-searchbar">
      <el-col :span="3" class="left">
        <nuxt-link to="/">
          <img src="//s0.meituan.net/bs/fe-web-meituan/e5eeaef/img/logo.png" alt="美团">
        </nuxt-link>
      </el-col>
      <el-col :span="15" class="center">
        <div class="wrapper">
          <el-input
            v-model="search"
            placeholder="搜索商家或地点"
            @focus="focus"
            @blur="blur"
            @input="input"/>
          <button class="el-button el-button--primary">
            <i class="el-icon-search"/>
          </button>
          <dl v-if="isHotPlace" class="hotPlace">
            <dt>热门搜索</dt>
            <dd v-for="(item, index) in $store.state.search.hotPlace.slice(0, 4)" :key="index">
              <a :href="'/products?keyword=' + encodeURIComponent(item.name)">{{ item.name }}</a>
            </dd>
          </dl>
          <dl v-if="isSearchList" class="searchList">
            <dd v-for="(item, index) in searchList" :key="index">
              <a :href="'/products?keyword=' + encodeURIComponent(item.name)">{{ item.name }}</a>
            </dd>
          </dl>
        </div>
        <div class="suggset">
          <a v-for="(item, index) in $store.state.search.hotPlace.slice(0, 4)"
             :key="index"
             :href="'/products?keyword=' + encodeURIComponent(item.name)">{{ item.name }}</a>
        </div>
        <ul class="nav">
          <li>
            <nuxt-link to="/" class="takeout">美团外卖</nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="movie">猫眼电影</nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="hotel">美团酒店</nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="apartment">民宿/公寓</nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="business">商家入驻</nuxt-link>
          </li>
        </ul>
      </el-col>
      <el-col :span="6" class="right">
        <ul class="security">
          <li>
            <i class="refund"/>
            <p class="txt">随时退</p>
          </li>
          <li>
            <i class="single"/>
            <p class="txt">不满意免单</p>
          </li>
          <li>
            <i class="overdue"/>
            <p class="txt">过期退</p>
          </li>
        </ul>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import _ from 'lodash'
export default {
  data(){
    return {
      search: '',
      isFocus: false,
      hotPlace: [],
      searchList: [],
      inputValue: false
    }
  },
  computed:{
    /**
     * 展示热门搜索
     */
    isHotPlace: function () {
      return this.isFocus && !this.search
    },
    /**
     * 展示搜索记录
     */
    isSearchList: function () {
      return this.isFocus && this.inputValue
    }
  },
  methods:{
    /**
     * 点击输入框，展开热门搜索
     */
    focus: function () {
      this.isFocus = true
    },
    /**
     * 输入框失去焦点
     */
    blur: function () {
      setTimeout(() => {
        this.isFocus = false
        this.inputValue = false
      }, 200)
    },
    /**
     * 输入框输入的时候会触发到该事件
     * _.debounce延时函数
     */
    input: _.debounce(async function(){
      this.searchList = []
      const city = this.$store.state.geo.position.city.replace('市', '')
      const { data: { top }} = await this.$axios.get('/search/top', {
        params: { // pass to search.js parameter
          input: this.search,
          city
        }
      })
      this.searchList = top.slice(0, 10)
      if (this.search === '') {
        this.inputValue = false
      } else {
        this.inputValue = true
      }
    }, 300)
  }
}
</script>
<style lang="css">
</style>

